package com.wc.alorithm_blue_bridge._DFS.迷宫2;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/12/18-23:10
 */
import java.util.HashMap;

/**
 * @Author congege
 * @Description 迷宫
 * https://www.lanqiao.cn/problems/641/learning/?page=1&first_category_id=1
 * @Date Created in 2023/12/18-22:34
 */
public class Main2 {
    static HashMap<Character, Integer> opNum = new HashMap<>();
    static int[] positionX = new int[]{-1, 0, 1, 0};
    static int[] positionY = new int[]{0, 1, 0, -1};
    // ans[i][j]=1 代表没走过，  大于1的能%2=0说明上边能走通，%3=0的说明右边能走通
    static int[][] ans = new int[10][10];
    static char[][] map = new char[10][10];

    static void init() {
        opNum.put('U', 0);
        opNum.put('R', 1);
        opNum.put('D', 2);
        opNum.put('L', 3);
    }

    /**
     * @param x  当前位置x
     * @param y  当前位置y
     */
    public static boolean dfs(int x, int y) {
        if (isBorder(x, y)) {
            return true;
        }

        int opIdx = opNum.get(map[x][y]);
        if (ans[x][y] == 1) {
            return true;
        } else if (ans[x][y] == -1) {
            return false;
        }
        ans[x][y] = -1;
        boolean isRun = dfs(x + positionX[opIdx], y + positionY[opIdx]);

        if (isRun) {
            ans[x][y] = 1;
            return true;
        }
        return false;
    }

    /**
     * 判断 x, y 是否为边界
     *
     * @param x
     * @param y
     * @return
     */
    public static boolean isBorder(int x, int y) {
        return x < 0 || x == 10 || y < 0 || y == 10;
    }

    public static void main(String[] args) {
        String mapStr = "UDDLUULRUL\n" +
                "UURLLLRRRU\n" +
                "RRUURLDLRD\n" +
                "RUDDDDUUUU\n" +
                "URUDLLRRUU\n" +
                "DURLRLDLRL\n" +
                "ULLURLLRDU\n" +
                "RDLULLRDDD\n" +
                "UUDDUDUDLL\n" +
                "ULRDLUURRR";
        String[] mapSS = mapStr.split("\\n");
        for (int i = 0; i < 10; i++) {
            map[i] = mapSS[i].toCharArray();
        }
        init();
        int ans = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (dfs(i, j)) {
                    ans++;
                }
            }
        }
        System.out.println(ans);
    }
}